package com.waijiaojun.tpo.web;

import com.waijiaojun.tpo.entity.Question;
import com.waijiaojun.tpo.entity.UserAnswers;
import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.UserCategoryService;
import com.waijiaojun.tpo.service.UserService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.JsonUtil;
import com.waijiaojun.tpo.utils.UserUtil;
import com.waijiaojun.tpo.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xvitcoder
 * Date: 12/20/12
 * Time: 5:27 PM
 */
@Controller
@RequestMapping("/exercise")
public class ExerciseController {
    @Autowired
    private UserCategoryService userCategoryService;

    private static Logger logger = LoggerFactory.getLogger(ExerciseController.class);

    // 练习地图
    @RequestMapping("categories")
    @ResponseBody
    public RestResponse categories() {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in );
            String openid = UserUtil.getCurrentUserOpenId();
            List<UserCategoryVo> data = userCategoryService.findCategoryWithUser(openid);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 点击关卡，保存状态，表示用户已开启该关卡
    @RequestMapping("categories/click/{categoryId}")
    @ResponseBody
    public RestResponse categoriesClick(@PathVariable("categoryId")Long categoryId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "categoryId:"+categoryId);
            String openid = UserUtil.getCurrentUserOpenId();
            userCategoryService.categoriesClick(openid,categoryId);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }


    // 分类详情
    @RequestMapping("categories/details/{categoryId}")
    @ResponseBody
    public RestResponse categoriesDetails(@PathVariable("categoryId")Long categoryId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "categoryId:"+categoryId);
            String openid = UserUtil.getCurrentUserOpenId();
            CategoryDetailsVo data = userCategoryService.findCategoriesDetailsWithUser(openid,categoryId);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 场景详情
    @RequestMapping("categories/scenario/detail/{scenarioId}")
    @ResponseBody
    public RestResponse scenarioDetail(@PathVariable("scenarioId")Long scenarioId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "scenarioId:"+scenarioId);
            String openid = UserUtil.getCurrentUserOpenId();
            ScenarioDetailVo data = userCategoryService.scenarioDetail(openid,scenarioId);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 点击场景首页页面上面的3个按钮，保存开启按钮状态
    @RequestMapping("categories/scenario/details/clickButton/{scenarioId}/{number}")
    @ResponseBody
    public RestResponse clickButton(@PathVariable("scenarioId")Long scenarioId,@PathVariable("number")Long number) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "scenarioId:"+scenarioId +",number:"+number);
            String openid = UserUtil.getCurrentUserOpenId();
            userCategoryService.clickButton(openid,number,scenarioId);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 预习
    @RequestMapping("categories/scenario/prepare/{scenarioId}")
    @ResponseBody
    public RestResponse prepare(@PathVariable("scenarioId")Long scenarioId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "scenarioId:"+scenarioId);
            String openid = UserUtil.getCurrentUserOpenId();
            PrepareVo data = userCategoryService.prepare(scenarioId);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 内容回顾
    @RequestMapping("categories/scenario/review/{scenarioId}")
    @ResponseBody
    public RestResponse review(@PathVariable("scenarioId")Long scenarioId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "scenarioId:"+scenarioId);
            ReviewVo data = userCategoryService.review(scenarioId);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 做练习-1-音频材料
    @RequestMapping("categories/scenario/exec/audio/{scenarioId}")
    @ResponseBody
    public RestResponse execAudio(@PathVariable("scenarioId")Long scenarioId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "scenarioId:"+scenarioId);
            ExecAudioVo data = userCategoryService.execAudio(scenarioId);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 做练习-2-题目
    @RequestMapping("categories/scenario/exec/question/{scenarioId}")
    @ResponseBody
    public RestResponse execQuestion(@PathVariable("scenarioId")Long scenarioId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "scenarioId:"+scenarioId);
            List<Question> data = userCategoryService.execQuestion(scenarioId);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 做练习-3-题目-用户提交答案并显示答题结果
    @RequestMapping("categories/scenario/exec/question/saveUserAnswer/{scenarioId}")
    @ResponseBody
    public RestResponse execSaveUserAnswer(@RequestBody List<UserAnswers> userAnswersList,@PathVariable("scenarioId")Long scenarioId) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "scenarioId:"+scenarioId +"userAnswersList:"+JsonUtil.toJson(userAnswersList));
            String openid = UserUtil.getCurrentUserOpenId();
            List<QuestionAndUserAnswerVo> data = userCategoryService.execSaveUserAnswer(openid,userAnswersList,scenarioId);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 错题统计
    @RequestMapping("statistics/errorsQuestion")
    @ResponseBody
    public RestResponse statisticsErrorsQuestion() {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in );
            String openid = UserUtil.getCurrentUserOpenId();
            StatisticsErrorsQuestionResultVo data = userCategoryService.statisticsErrorsQuestion(openid);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }
}
